/*global location: false, YAHOO: false, jQuery: false, SelectList: false, not_default_columns: false, window: false, Results: false, RECORDS_PER_PAGE: false, ResultsColumnDefs: false, emptyMessage: false */

/*members Event, MSG_EMPTY, Paginator, ScrollingDataTable, ac_param, 
    addListener, generateRequest, indexOf, initialLoad, initialRequest, 
    limit, newValue, paginator, records, replace, rowsPerPage, search, soft, 
    start, subscribe, text, type, util, widget, width
*/

/* Freebase-suggest options */

var filter_options = {},
    initialLoad = false,
    ResultsDataTableDeferred, 
    location_search = location.search,
    bv_options = {
        soft: true,
        ac_param: {
            type: "/base/bioventurist/bv_medical_condition",
            start: 0,
            limit: 30
        }
    };

/* begin: requestBuilder builds string for GET query. This string contains filter options and additional columns */

function requestBuilder(oState, oSelf) {
    
    var s = '?', i, ls;
    if ('' === location_search) {
        
        for (i in filter_options) {
            if (filter_options[i]) {
                if (i.indexOf('col_') === 0) {                        
                    s += i.replace('&', '%26').replace(' ', '+') + '&';
                } else {
                    s += 'filter_by_' + i + '=' + filter_options[i].replace(' ', '+').replace('&', '%26') + '&';
                }
            }
        }            
        return s;
        
    } else {
        ls = location_search;
        location_search = '';
        return ls;
    }
}
/* end: requestBuilder */


YAHOO.util.Event.addListener(window, "load", function () {


    /* begin: Paginator */
    var paginator = new YAHOO.widget.Paginator({
        rowsPerPage: RECORDS_PER_PAGE
    });

    /* Change numbers of current showing records in paragraph "Showing ... of ..." */
    paginator.subscribe('changeRequest', function (event) {
        var start_index = event.records[0] + 1, end_index = event.records[1] + 1;     
        jQuery('#records_index').text(start_index + '-' + end_index + ' of ');
        return true;
    });

    /* Change count of records in paragraph "Showing ... of ..." */
    paginator.subscribe('beforeTotalRecordsChange', function (event) {
        var count = event.newValue;
        jQuery('#total_records').text(count);
        return true;
    });
    /* end: Paginator */

    /* If we request page with params (e.g. whe user copy URL from 'Share') do initial load of data */

    if (location_search !== '') {
        initialLoad = true;
    }

    ResultsDataTableDeferred = new YAHOO.widget.ScrollingDataTable("results", ResultsColumnDefs, Results, {
        paginator: paginator,
        generateRequest: requestBuilder,
        width: "889px",
        initialLoad: initialLoad,
        initialRequest: requestBuilder(),
        MSG_EMPTY: emptyMessage
    });

});
